import Vue from 'vue'
import Router from 'vue-router'
// import store from '../store/store'
import NProgress from 'nprogress' //进度条
import 'nprogress/nprogress.css'

Vue.use(Router)
// 路由懒加载
const getComponent = (name, component) => () =>
    import(`@/views/${name}/${component}.vue`);
const myRouter = new Router({
    routes: [{
            path: '/index',
            name: 'index',
            component: getComponent('index', 'index'),
            meta: {
                title: '数据库管理平台'
            },
            children: [{
                path: 'newLink',
                name: 'newLink',
                component: getComponent('newLink', 'newLink'),
                meta: {
                    title: '新建连接'
                }
            }, {
                path: 'newTable',
                name: 'newTable',
                component: getComponent('newTable', 'newTable'),
                meta: {
                    title: '新建表'
                }
            }, {
                path: 'editTable',
                name: 'editTable',
                component: getComponent('editTable', 'editTable'),
                meta: {
                    title: '编辑表'
                }
            }, {
                path: 'newQuery',
                name: 'newQuery',
                component: getComponent('newQuery', 'newQuery'),
                meta: {
                    title: '新建查询'
                }
            }, {
                path: 'userManage',
                name: 'userManage',
                component: getComponent('userManage', 'userManage'),
                meta: {
                    title: '用户管理'
                }
            }]
        },
        {
            path: '/',
            redirect: '/index',
            meta: {
                title: '数据库管理平台'
            }
        },

    ]
})

//判断是否存在token
myRouter.beforeEach((to, from, next) => {
    NProgress.start()
    // if (to.path !== '/login' && !store.state.token) {
    //     next('/login')
    //     NProgress.done() // 结束Progress
    // } else {
    //     next();
    // }
    // if (to.meta.roles) {
    //     to.meta.roles.includes(...store.getters.roles) ? next() : next('/404')
    // } else {
    //     next();
    // }
    next();
})

myRouter.afterEach(() => {
    NProgress.done() // 结束Progress
})
export default myRouter
